'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var unionGroupModel = require('../../../../models/unionGroupModel');
var roleService = require('../../../../services/roleService');
var filterSensitiveService = require('../../../../services/filterSensitiveService');
var PlayerManager = require('../../../../libs/playerManager');
var constantUtil = require('../../../../util/constantUtil');
var unionPositionType = require('../../../../consts/unionPositionType');

/**
 * 创建公会
 */
module.exports = function (selfApp, selfGlobal, msg, session, next) {
    var role_id = session.uid;
    var union_name = msg.union_name;
    var union_announce = msg.union_announce;
    var union_flag_id = msg.union_flag_id;
    var limit_lv = msg.limit_lv;

    if (!union_name) {
        return next(null, {
            code: Code.UNION_UNION_NAME_NULL
        });
    }

    // 是否有敏感詞彙
    if (!filterSensitiveService.detect(union_name)) {
        return next(null, {
            code: Code.UNION_UNION_NAME_NOT
        });
    }

    if (!union_announce) {
        return next(null, {
            code: Code.UNION_MSG_NULL
        });
    }

    // 是否有敏感詞彙
    if (!filterSensitiveService.detect(union_announce)) {
        return next(null, {
            code: Code.UNION_ANNOUNCE_CONTAIN_SENSITIVE
        });
    }

    if (!union_flag_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    if (!limit_lv) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var role_model;
    var union_model;
    var union_group_model;

    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(
                    ['role', 'unionGroup', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;

                // 是否达到创建公会等级
                if (role_model.getLv() < constantUtil.value('found_union_lvl_limit')) {
                    return next(null, {
                        code: Code.USER_LEVEL_NOT_ENOUGH
                    });
                }

                // 判断玩家是否已经有公会
                if (role_model.getUnionId() !== 0) {
                    return next(null, {
                        code: Code.UNION_IS_JOIN
                    });
                }

                // 扣除钻石
                if (!roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_UNION_CREATE, constantUtil.value('found_union_cost'))) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }
                unionGroupModel.getByName(union_name, cb);
            },
            function (model, cb) {
                if (!!model) {
                    return next(null, {
                        code: Code.UNION_NAME_IS_EXIST
                    });
                }

                unionGroupModel.create(union_name, role_id, cb);
            },
            function (model, cb) {
                union_group_model = model;
                role_model.setUnionId(union_group_model.getUnionId());

                // 角色表设置联盟信息
                role_model.union_name = union_group_model.name;
                role_model.union_flag = union_group_model.union_flag;

                // 加入联盟公告
                union_group_model.setUnionAnnounce(union_announce);

                // 加入联盟旗帜
                union_group_model.setUnionFlag(union_flag_id);

                // 设置加入等级
                union_group_model.setUnionJoinLimirLevel(limit_lv);

                union_group_model.addUnionUser(role_id, 1);

                // 加入联盟聊天聊天通道
                selfApp.rpc.connector.entryRemote.addUnionChannel(session, union_group_model.getUnionId(), role_id, function (err) {
                    if (!!err) {
                        console.err(err);
                    }
                });
                player_manager.save(cb);
            },
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            union_group_model.save();
            return next(null, {
                code: Code.OK
            });
        }
    );
};
